package cn.big.bigd.ybp.util;

import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.hibernate.loader.plan.spi.Return;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 8/16/2016.
 */
public class MessageUtil {

    public static  final  String MESSAGE_TEXT="text";
    public static  final  String MESSAGE_IMAGE="image";
    public static  final  String MESSAGE_VOICE="voice";
    public static  final  String MESSAGE_VIDEO="video";
    public static  final  String MESSAGE_LINK="link";
    public static  final  String MESSAGE_LOCATION="location";
    public static  final  String MESSAGE_EVENT="event";
    public static  final  String MESSAGE_SUBSCRIBE="subscribe";
    public static  final  String MESSAGE_UNSUBSCRIBE="unsubscribe";
    public static  final  String MESSAGE_CLICK ="click";
    public static  final  String MESSAGE_VIEW="view";


    public static Map<String,String> xmlToMap(HttpServletRequest request) throws Exception {
        Map<String,String> map = new HashMap<String,String>();
        SAXReader reader= new SAXReader();
        InputStream ins =request.getInputStream();
        Document doc =reader.read(ins);
        Element root =doc.getRootElement();
        List<Element> list = root.elements();
        for (Element e:list) {
            map.put(e.getName(),e.getText());


        }
        ins.close();
        return map;

    }

    public static  String textMessageToXml(TextMessage textMessage){
        XStream xstream =new XStream();
        xstream.alias("xml",textMessage.getClass());
        return xstream.toXML(textMessage);

    }


    public static  String initText(String toUserName,String fromUserName,String content){
        TextMessage text = new TextMessage();
        text.setFromUserName(toUserName);
        text.setToUserName(fromUserName);
        text.setCreateTime(new Date().getTime());
        text.setMsgType(MESSAGE_TEXT);
        text.setContent(content);
        return MessageUtil.textMessageToXml(text);
    }
    /**
     * 主菜单
     * @return
     */
    public static  String menuText(){
        StringBuffer sb=new StringBuffer();
        sb.append("欢迎您的关注，请按照菜单提示进行操作：\n\n");
        sb.append("1、 梁少华\n");
        sb.append("2、 王凡\n\n");
        sb.append("回复？调出此菜单。");
        return sb.toString();
    }

    public static  String firstMenu(){
        StringBuffer sb=new StringBuffer();
        sb.append("梁少华是个大美女....\n");
        sb.append("你同意吗？");
        return sb.toString();
    }

    public static  String secondMenu(){
        StringBuffer sb=new StringBuffer();
        sb.append("王凡是个大帅哥....\n");
        sb.append("你同意吗？");
        return sb.toString();
    }
}
